package cn.yuysaln.trigger.listener;

import cn.yuysaln.domain.activity.service.IRaffleActivitySkuStockService;
import cn.yuysaln.types.event.BaseEvent;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Slf4j
@Component
public class ActivitySkuStockZeroCustomer {
    @Value("${spring.rabbitmq.topic.activity_sku_stock_zero}")
    private String topic;

    @Resource
    private IRaffleActivitySkuStockService skuStock;
    @RabbitListener(queuesToDeclare = @Queue(value = "activity_sku_stock_zero"))
    public void listen(String message) {
        try {
            log.info("【RabbitMQ】---已收到sku库存消耗为0  topic: {} message: {}", topic, message);
            // 转换对象
            BaseEvent.EventMessage<Long> eventMessage = JSON.parseObject(message, new TypeReference<BaseEvent.EventMessage<Long>>() {
            }.getType());
            Long sku = eventMessage.getData();
            //先后清空指定sku的库存和队列信息
            skuStock.clearActivitySkuStock(sku);
            skuStock.clearQueueValue();
            log.info("【RabbitMQ】---清空sku相关库存同步操作  topic: {} message: {}", topic, message);
        }catch (Exception e){
            log.error("【RabbitMQ】---sku相关资源清除失败  topic: {} message: {}", topic, message);
            throw e;

        }

    }
}
